#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"

namespace cgc 
{
	string::iterator string::begin()
	{
		return  _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}
	string::const_iterator string::begin() const
	{
		return _str;
	}

	string::const_iterator string::end() const
	{
		return _str + _size;
	}
	size_t string::size() const
	{
		return _size;
	}
	const char* string::c_str() const
	{
		return _str;
	}
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];
		memcpy(_str, str, _size + 1);
	}
	string::string(const string& s)
	{
		string temp(s._str);
		swap(temp);
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
	char string::operator[](size_t i)
	{
		return _str[i];
	}
	const char string::operator[](size_t i) const
	{
		return _str[i];
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n + 1];
			memcpy(temp, _str, _size + 1);
			delete[] _str;
			_str = temp;
			_capacity = n;
		}
	}
	void string::push_back(char ch)
	{
		if (_size >= _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}
		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			int newcapacity = _size + len > 2 * _capacity ? _size + len : 2 * _capacity;
			reserve(newcapacity);
		}
		memcpy(_str + _size, str, len + 1);
		_size += len;
	}
	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	void string::pop_back()
	{
		assert(_size > 0);
		_str[--_size] = '\0';
	}
	string& string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size >= _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[pos] = ch;
		_size++;
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			int newcapacity = _size + len > 2 * _capacity ? _size + len : 2 * _capacity;
			reserve(newcapacity);
		}
		size_t end = _size + len;
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			end--;
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
		_size += len;
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos >= 0 && pos < _size);

		if (len == npos || (_size - pos) <= len)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			size_t i = pos + len;
			memmove(_str + pos, _str + i, _size - i + 1);
			_size -= len;
		}
		return *this;
	}
	size_t string::find(char ch, size_t pos) const
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}

		return npos;
	}
	size_t string::find(const char* str, size_t pos) const
	{
		const char* p = strstr(_str + pos, str);
		if (p == nullptr)
		{
			return npos;
		}
		else
		{
			return p - _str;
		}
	}
	string string::substr(size_t pos, size_t len) const
	{
		if (len == npos || (_size - pos) <= len)
		{
			len = _size - pos;
		}
		string ret;
		ret.reserve(len);
		for (int i = 0; i < len; i++)
		{
			ret += _str[pos + i];
		}
		return ret;
	}
	bool string::operator<(const string& s) const
	{
		size_t i1 = 0;
		size_t i2 = 0;
		while (i1 < _size && i2 < s.size())
		{
			if (_str[i1] < s._str[i2])
			{
				return true;
			}
			else if(_str[i1] > s._str[i2])
			{
				return false;
			}
			else
			{
				i1++;
				i2++;
			}
		}

		return i2 < s.size();
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s) const
	{
		return !(*this < s);
	}
	bool string::operator==(const string& s) const
	{
		size_t i1 = 0;
		size_t i2 = 0;
		while (i1 < _size && i2 < s._size)
		{
			if (_str[i1] != s._str[i2])
			{
				return false;
			}
			else
			{
				i1++;
				i2++;
			}
		}
		return i1 == _size && i2 == s._size;
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	string& string::operator=(string s)
	{;
		swap(s);
		return *this;
	}
	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	const size_t string::npos = -1;
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char buff[128];
		int i = 0;
		char ch = in.get();
		while (ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}
	istream& getline(istream& in, string& str, char delim)
	{
		str.clear();

		char buff[128];
		int i = 0;

		char ch = in.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			str += buff;
		}

		return in;
	}
}